package project.teg.games.game.gestures;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import project.teg.R;
import project.teg.common.types.GestureShapeType;
import project.teg.common.types.ScreenType;
import project.teg.configuration.Gestures;
import project.teg.configuration.Global;
import project.teg.database.DBHelper;
import android.app.Activity;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.Chronometer;

public class GameActivity extends Activity implements OnTouchListener {

	GameView gameView;
	GameController gameController;
	List<Measurement> measurements;
	Measurement currentmeasurement;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.main);

		// gameView settings
		gameController = new GameController();
		getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

		setContentView(R.layout.main);
		gameView = (GameView) findViewById(R.id.GameView01);
		gameView.setOnTouchListener(this);

		Global.GetAllValues(this);

		// measurement settins
		measurements = new ArrayList<Measurement>();
	}

	int br = 1;

	Chronometer c;
	PointF lastPointInPath;

	@Override
	public boolean onTouch(View v, MotionEvent event) {

		if (gameView.currentScreen == ScreenType.WelcomeScreen) {
			// gameView settings
			gameView.currentScreen = ScreenType.NextLevel;
			gameView.gesturePath.calculateNewCheckPoints();
			gameView.invalidate();
			c = (Chronometer) findViewById(R.id.Chronometer01);
			c.start();

			// measurement settins
			createInitialMeasurement();

			return false;
		} else if (gameView.currentScreen == ScreenType.NextLevel) {

			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				// gameView settings
				gameView.interactiveObject = new InteractiveObject(new PointF(
						event.getX(), event.getY()), new PointF(30, 30));

				gameView.interactiveObjevtPath.moveTo(event.getX(),
						event.getY());

				// measurement settins

				lastPointInPath = new PointF(event.getX(), event.getY());
				currentmeasurement.setTimeSelect(new Date());
				Log.e("TIME SELECT", currentmeasurement.getTimeSelect()
						.toString());
				if (Methods
						.checkIfCheckPointIsNear(
								gameView.gesturePath.checkPoints
										.get(gameView.gesturePath.nextCheckpointIndexToHit),
								new PointF(event.getX(), event.getY()))) {
					gameView.gesturePath.nextCheckpointIndexToHit++;
				}

			} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
				// gameView settings
				gameView.interactiveObject.locationCenter.x = event.getX();
				gameView.interactiveObject.locationCenter.y = event.getY();
				gameView.interactiveObjevtPath.lineTo(event.getX(),
						event.getY());

				// measurement settins
				currentmeasurement.pathLength += Methods
						.calculateDistance(
								new PointF(event.getX(), event.getY()),
								lastPointInPath);
				lastPointInPath.x = event.getX();
				lastPointInPath.y = event.getY();
				if (gameView.gesturePath.checkPoints.size() > gameView.gesturePath.nextCheckpointIndexToHit) {
					if (Methods
							.checkIfCheckPointIsNear(
									gameView.gesturePath.checkPoints
											.get(gameView.gesturePath.nextCheckpointIndexToHit),
									new PointF(event.getX(), event.getY()))) {
						gameView.gesturePath.nextCheckpointIndexToHit++;
					}
				}

			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				// measurement settins
				currentmeasurement.setTimeActionEnd(new Date());
				Log.e("ACTION END", currentmeasurement.getTimeActionEnd()
						.toString());
				currentmeasurement.intervalLevelStartToSelect = new Date(
						currentmeasurement.getTimeSelect().getTime()
								- currentmeasurement.getTimeLevelStart()
										.getTime()).getTime();
				currentmeasurement.intervalSelectToActionEnd = new Date(
						currentmeasurement.getTimeActionEnd().getTime()
								- currentmeasurement.getTimeSelect().getTime())
						.getTime();

				currentmeasurement.isTestSuccesful = true;
				for (CheckPoint c : gameView.gesturePath.checkPoints) {
					if (!c.isHit) {
						currentmeasurement.isTestSuccesful = false;
						break;
					}
				}

				// gameView settings
				br++;
				gameView.gesturePath.calculateNewCheckPoints();
				gameView.interactiveObject = null;
				gameView.interactiveObjevtPath.reset();

				// measurement settins

				// SAVE MEASUREMENT!!:
				measurements.add(currentmeasurement);

				// NEWMEASSUREMENT
				if (br <= Global.maxLevels)
					createInitialMeasurement();

			}
			if (br > Global.maxLevels) {
				gameView.currentScreen = ScreenType.GameOverScreen;

				DBHelper db = new DBHelper(this);
				for (Measurement m : measurements) {
					db.insertMeasurement(m);
				}
				db.close();
				measurements.clear();
			}
			gameView.invalidate();
			if (br <= Global.maxLevels) {

			}
			return true;

		} else { // if (gameView.currentScreen == ScreenType.GameOverScreen) {

			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				// gameView settings
				this.finish();
			}
			return true;
		}
	}

	private void createInitialMeasurement() {
		currentmeasurement = new Measurement();
		// currentmeasurement.timeCreated++
		currentmeasurement.backgroundColor = Gestures.backgroundColor;
		// currentmeasurement.beginCheckpointBorder+
		currentmeasurement.beginCheckPointColor = Gestures.beginCheckPointColor;
		// currentmeasurement.beginPathLocation+
		// currentmeasurement.beginPathScreenField+
		currentmeasurement.checkpointNumber = Gestures.numberOfCheckpoints;
		currentmeasurement.deviceType = Global.deviceType;
		currentmeasurement.dpi = Global.dpi;
		// currentmeasurement.dragLength+
		// currentmeasurement.endCheckpointBorder+
		currentmeasurement.endCheckpointColor = Gestures.endCheckpointColor;
		// currentmeasurement.endPathLocation+
		// currentmeasurement.endPathScreenField+
		currentmeasurement.gestureShapeType = Gestures.gestureShapeType;
		currentmeasurement.gestureType = Gestures.gestureType;
		currentmeasurement.handlingType = Global.handlingType;
		currentmeasurement.internalCheckpointColor = Gestures.internalCheckpointColor;
		// currentmeasurement.intervalLevelStartToSelect+
		// currentmeasurement.intervalSelectToActionEnd+
		currentmeasurement.isBackgroundSolidColor = Gestures.isBackgroundSolidColor;
		currentmeasurement.isCheckpointIndexVisible = Gestures.isCheckpointIndexVisible;
		currentmeasurement.isPathVisible = Gestures.isPathVisible;
		// currentmeasurement.isTestSuccesful
		currentmeasurement.pathColor = Gestures.pathColor;
		// currentmeasurement.pathLength
		// currentmeasurement.score
		currentmeasurement.setScreenSizePx(Global.screenSizePx);
		// currentmeasurement.timeActionEnd++
		// currentmeasurement.timeLevelStart++
		// currentmeasurement.timeSelect++

		currentmeasurement.beginCheckpointBorder = Methods
				.findClosestBorder(gameView.gesturePath.checkPoints.get(0).location);
		currentmeasurement
				.setBeginPathLocation(gameView.gesturePath.checkPoints.get(0).location);
		currentmeasurement.beginPathScreenField = Methods
				.determinePointScreenFiled(gameView.gesturePath.checkPoints
						.get(0).location);
		currentmeasurement.dragLength = 0;
		currentmeasurement.endCheckpointBorder = Methods
				.findClosestBorder(gameView.gesturePath.checkPoints
						.get(gameView.gesturePath.checkPoints.size() - 1).location);
		currentmeasurement.setEndPathLocation(gameView.gesturePath.checkPoints
				.get(gameView.gesturePath.checkPoints.size() - 1).location);
		currentmeasurement.endPathScreenField = Methods
				.determinePointScreenFiled(gameView.gesturePath.checkPoints
						.get(gameView.gesturePath.checkPoints.size() - 1).location);
		gameView.gesturePath.nextCheckpointIndexToHit = 0;
		currentmeasurement.setTimeLevelStart(new Date());
		Log.e("LEVEL START", currentmeasurement.getTimeLevelStart().toString());
	}

}
